#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <float.h>

#define LIST 201

#if(LIST == 201)
// 2-1
#include <stdio.h>
int main(void)
{
    int x = 3/2;
    float y = 3.0/2.0;
    printf("3/2 = %d\r\n3.0/2.0 = %1.1f\n",x,y);
    return 0;
}

#elif(LIST == 202)
// 2-2
float Q_rsqrt( float number )
{
    long i;
    float x2, y;
    const float threehalfs = 1.5F;
    x2 = number * 0.5F;
    y  = number;
    i  = * ( long * ) &y;
    i  = 0x5f3759df - ( i >> 1 );
    y  = * ( float * ) &i;
    // 第一次迭�?
    y  = y * ( threehalfs - ( x2 * y * y ) );
    // 第二迭代
    // y  = y * ( threehalfs - ( x2 * y * y ) );
    return y;
}

// 2-3
#include <stdio.h>
int main (void)
{
    int x=0;
    int y=0x80000000;
    char buf[sizeof("128")];
    x=sprintf(buf,"%u",y>>24);
    if(x==-1||x>=sizeof(buf))
    {
            // 错误处理
    }
    printf(buf);
    return 0;
}

// 2-4
#include <stdio.h>
int main (void)
{
    int x=0;
    unsigned int y=0x80000000;
    char buf[sizeof("128")];
    x=sprintf(buf,"%u",y>>24);
    if(x==-1||x>=sizeof(buf))
    {
        // 错误处理
    }
    printf(buf);
    return 0;
}

// 2-5
#include <stdio.h>
int main (void)
{
    unsigned int x = 0x1101;
    unsigned int y = 0x1100;
    /*逻辑操作*/
    printf("sizeof(x || y): %d\n", sizeof(x || y));
    if(x||y)
    {
            printf("x || y : %d(True) \n", x||y);
    }
    else
    {
            printf("x || y : %d(False) \n", x||y);
    }
    /*位操�?*/
    printf("sizeof(x | y): %d\n", sizeof(x | y));
    printf("x | y : %x \n", x|y);
    return 0;
}

// 2-6
#include <stdio.h>
int main (void)
{
    unsigned int x = 0x1101;
    unsigned int y = 0x1100;
    /*逻辑操作*/
    printf("sizeof(x && y): %d\n", sizeof(x && y));
    if(x&&y)
    {
            printf("x && y : %d(True) \n", x&&y);
    }
    else
    {
            printf("x && y : %d(False) \n", x&&y);
    }
    /*位操�?*/
    printf("sizeof(x & y): %d\n", sizeof(x & y));
    printf("x & y : %x \n", x&y);
    return 0;
}


// 3-1
#include <stdio.h>
#include <math.h>
#define EPSILON 0.000000001
int main (void)
{
    double a = 10.22222225;
    double b = 10.22222222;
    double c = 0.0000001;
    if ( fabs(a-b) <= EPSILON )
    {
            printf("a:%.12f == b:%2.12f,
                        精度�?%.12f \n",a,b,EPSILON);
    }
    else
    {
            printf("a:%.12f != b:%2.12f,
                       精度�?%.12f \n",a,b,EPSILON);
    }
    if (fabs(c) <= EPSILON)
    {
            printf("c:%.12f == 0, 精度�?%.12f \n",c,EPSILON);
    }
    else
    {
            printf("c:%.12f != 0, 精度�?%.12f \n",c, EPSILON);
    }
    return 0;
}

// 3-2
#include <stdio.h>
void print_week(unsigned int day);
void print_week(unsigned int day)
{
    switch(day)
    {
    case 1:
            printf("Monday\n");
            break;
    case 2:
            printf("Tuesday\n");
            break;
    case 3:
            printf("Wednesday\n");
            break;
    case 4:
            printf("Thursday\n");
            break;
    case 5:
            printf("Friday\n");
            break;
    case 6:
            printf("Saturday\n");
            break;
    case 7:
            printf("Sunday\n");
            break;
    default:
            printf("error\n");
            break;
    }
}
int main (void)
{
    print_week(3);
    return 0;
}

// 3-3
void print_week(unsigned int day)
{
    switch(day)
    {
    case 1:
            printf("Monday\n");
    case 2:
            printf("Tuesday\n");
    case 3:
            printf("Wednesday\n");
    case 4:
            printf("Thursday\n");
    case 5:
            printf("Friday\n");
            break;
    case 6:
            printf("Saturday\n");
            break;
    case 7:
            printf("Sunday\n");
            break;
    default:
            printf("error\n");
            break;
    }
}
int main (void)
{
    print_week(2);
    return 0;
}

// 4-1
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
typedef struct node
{
    ElementType data;
    struct node *next;
}StackNode, *LinkStack;
void InvertedSequence(int num)
{
    int i=0;
    int result=0;
    LinkStack ls;
    // 初始�?
    ls = (LinkStack)malloc(sizeof(StackNode));
    ls->next = NULL;
    printf(“数据输入为:\n�?);
    for(i=0; i<num; i++)
    {
            // 入栈
            StackNode *temp;
            temp = (StackNode *)malloc(sizeof(StackNode));
            if(temp != NULL)
            {
                    temp->data = i;
                    temp->next = ls->next;
                    ls->next = temp;
                    printf("%d ",i);
            }
    }
    printf("\n数据输出�?:\n");
    while (ls->next != NULL)
    {
            // 出栈
            StackNode *temp = ls->next;
            result = temp->data;
            ls->next = temp->next;
            free(temp);
            printf("%d ",result);
    }
    printf("\n");
}
int main(void)
{
    InvertedSequence(20);
    return 0;
}

// 4-2
typedef int BOOL;
#define TRUE 1
#define FALSE 0
#define STACK_SIZE 100
typedef int ElementType;
typedef struct node
{
    ElementType data;
    struct node *next;
}StackNode, *LinkStack;
// 初始�?
void InitStack(LinkStack ls)
{
    ls->next = NULL;
}
// 是否为空
BOOL IsEmpty(LinkStack ls)
{
    if(ls->next == NULL)
    {
            return TRUE;
    }
    else
    {
            return FALSE;
    }
}
// 入栈
BOOL Push(LinkStack ls, ElementType element)
{
    StackNode *temp;
    temp = (StackNode *)malloc(sizeof(StackNode));
    if(temp == NULL)
    {
            return FALSE;
    }
    temp->data = element;
    temp->next = ls->next;
    ls->next = temp;
    return TRUE;
}
// 出栈
BOOL Pop(LinkStack ls, ElementType *element)
{
    if(IsEmpty(ls))
    {
            return FALSE;
    }
    else
    {
            StackNode *temp = ls->next;
            *element = temp->data;
            ls->next = temp->next;
            free(temp);
            return TRUE;
    }
}
void InvertedSequence(int num)
{
    int i=0;
    int result=0;
    LinkStack ls;
    ls = (LinkStack)malloc(sizeof(StackNode));
    // 初始�?
    InitStack(ls);
    printf("数据输入�?:\n");
    for(i=0; i<num; i++)
    {
            // 入栈
            Push(ls,i);
            printf("%d ",i);
    }
    printf("\n数据输出�?:\n");
    while (!IsEmpty(ls))
    {
            // 出栈
            Pop(ls,&result);
            printf("%d ",result);
    }
    printf("\n");
}

#endif